<cfcomponent displayname="UserEventDAO">


	<cfset variables.private = structNew() />

	<cffunction name="init" access="public" output="false" returntype="UserEventDAO">
		<cfargument name="KalendarConfig" type="any" required="true" hint="This is a reference to the main Kalendar Config component"/>
		<cfset variables.private.Config = arguments.KalendarConfig />
		<cfreturn this>
	</cffunction>
	
	<cffunction name="create" access="public" output="false" returntype="boolean">
		<cfargument name="Kalendar" type="any" required="true" />
		<cfset var qCreate = "" />
		<cfquery name="qCreate" datasource="#getConfig().getDSN()#">
			INSERT INTO UserEvents
				(
					UserEventID,
					UserID,
					EventID,
					UserRole,
					IsDeleted
				)
			VALUES
				(
					<cfqueryparam value="#CreateUUID()#" CFSQLType="cf_sql_varchar" />,
					<cfqueryparam value="#arguments.Kalendar.getUserID()#" CFSQLType="cf_sql_varchar" />,
					<cfqueryparam value="#arguments.Kalendar.getEventID()#" CFSQLType="cf_sql_varchar" />,
					<cfqueryparam value="#getRoleWeight(arguments.Kalendar.getRole())#" CFSQLType="cf_sql_int" />,
					<cfqueryparam value="#YesNoFormat(arguments.Kalendar.getIsDeleted())#" CFSQLType="cf_sql_bit" />
				)
		</cfquery>
		<cfreturn true />
	</cffunction>

	<cffunction name="read" access="public" output="false" returntype="void">
		<cfargument name="Kalendar" type="any" required="true" />
		<cfset var qRead = "" />
		<cfset var strReturn = structNew() />
		<cfquery name="qRead" datasource="#getConfig().getDSN()#">
			SELECT
				UserID,
				EntryID,
				UserRole,
				IsDeleted
			FROM	UserEvents
			WHERE	EventID = <cfqueryparam value="#arguments.Kalendar.getEventID()#" CFSQLType="cf_sql_varchar" />
		</cfquery>
		<cfif qRead.recordCount>
			<cfset strReturn = queryRowToStruct(qRead)>
			<cfset arguments.Kalendar.init(argumentCollection=strReturn)>
		</cfif>
	</cffunction>

	<cffunction name="update" access="public" output="false" returntype="boolean">
		<cfargument name="Kalendar" type="any" required="true" />
		<cfset var qUpdate = "" />
		<cfquery name="qUpdate" datasource="#getConfig().getDSN()#">
			UPDATE	UserEvents
			SET
				UserID = <cfqueryparam value="#arguments.Kalendar.getUserID()#" CFSQLType="cf_sql_varchar" />,
				EventID = <cfqueryparam value="#arguments.Kalendar.getEventID()#" CFSQLType="cf_sql_varchar" />,
				UserRole = <cfqueryparam value="#getRoleWeight(arguments.Kalendar.getRole())#" CFSQLType="cf_sql_int" />,
				IsDeleted = <cfqueryparam value="#YesNoFormat(arguments.Kalendar.getIsDeleted())#" CFSQLType="cf_sql_bit" />
			WHERE	EventID = <cfqueryparam value="#arguments.Kalendar.getEventID()#" CFSQLType="cf_sql_varchar" />
		</cfquery>
		<cfreturn true/>
	</cffunction>

	<cffunction name="getRoleWeight" access="private" returntype="numeric">
		<cfargument name="role" type="string" required="true"/>
		<cfset var roleWeight = 0/>
		<cfswitch expression="#arguments.role#">
			<cfcase value="mysource"><cfset roleWeight = 1/></cfcase>
			<cfcase value="dealer"><cfset roleWeight = 2/></cfcase>
			<cfcase value="admin"><cfset roleWeight = 3/></cfcase>
			<cfdefaultcase><cfset roleWeight = 0/></cfdefaultcase>
		</cfswitch>
		<cfreturn roleWeight/>
	</cffunction>

	<cffunction name="delete" access="public" output="false" returntype="boolean">
		<cfargument name="Kalendar" type="any" required="true" />

		<cfset var qDelete = "">
		<cftry>
			<cfquery name="qDelete" datasource="#getConfig().getDSN()#">
						
			UPDATE
				UserEvents
			SET
				IsDeleted = 1
			WHERE
				EventID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.Kalendar.getEventID()#" />
			</cfquery>
			<cfcatch type="database">
				<cfreturn false />
			</cfcatch>
		</cftry>
		<cfreturn true />
	</cffunction>

	<cffunction name="exists" access="public" output="false" returntype="boolean">
		<cfargument name="Kalendar" type="any" required="true" />

		<cfset var qExists = "">
		<cfquery name="qExists" datasource="#getConfig().getDSN()#" maxrows="1">
			SELECT count(1) as idexists
			FROM	UserEvents
			WHERE	EventID = <cfqueryparam value="#arguments.Kalendar.getEventID()#" CFSQLType="cf_sql_varchar" />
		</cfquery>

		<cfif qExists.idexists>
			<cfreturn true />
		<cfelse>
			<cfreturn false />
		</cfif>
	</cffunction>

	<cffunction name="save" access="public" output="false" returntype="boolean">
		<cfargument name="Kalendar" type="any" required="true" />
		
		<cfset var success = false />
		<cfif exists(arguments.Kalendar)>
			<cfset success = update(arguments.Kalendar) />
		<cfelse>
			<cfset success = create(arguments.Kalendar) />
		</cfif>
		
		<cfreturn success />
	</cffunction>

	<cffunction name="queryRowToStruct" access="private" output="false" returntype="struct">
		<cfargument name="qry" type="query" required="true">
		
		<cfscript>
			/**
			 * Makes a row of a query into a structure.
			 * 
			 * @param query 	 The query to work with. 
			 * @param row 	 Row number to check. Defaults to row 1. 
			 * @return Returns a structure. 
			 * @author Nathan Dintenfass (nathan@changemedia.com) 
			 * @version 1, December 11, 2001 
			 */
			//by default, do this to the first row of the query
			var row = 1;
			//a var for looping
			var ii = 1;
			//the cols to loop over
			var cols = listToArray(qry.columnList);
			//the struct to return
			var stReturn = structnew();
			//if there is a second argument, use that for the row number
			if(arrayLen(arguments) GT 1)
				row = arguments[2];
			//loop over the cols and build the struct from the query row
			for(ii = 1; ii lte arraylen(cols); ii = ii + 1){
				stReturn[cols[ii]] = qry[cols[ii]][row];
			}		
			//return the struct
			return stReturn;
		</cfscript>
	</cffunction>
	
	<cffunction name="getConfig" access="public" output="false" returntype="any">
		<cfreturn variables.private.Config />
	</cffunction>
</cfcomponent>